﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Admin.NET.Application.ServiceApi;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using SqlSugar;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 360服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings(ApplicationConst.GroupNameApi, Order = 100)]
public class car_360ServiceApi : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_360> _rep;
    public car_360ServiceApi(SqlSugarRepository<car_360> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public Excar_360Output Page(Excar_360Input input)
    {
        var query = _rep.AsQueryable()
                    .WhereIF(input.carId > 0, u => u.carId == input.carId)
                    .WhereIF(input.carSubId > 0, u => u.carSubId == input.carSubId)
                    .WhereIF(input.xilieId > 0, u => u.xilieId == input.xilieId)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.imageUrl), u => u.imageUrl.Contains(input.imageUrl.Trim()))
                    .WhereIF(input.clickCount > 0, u => u.clickCount == input.clickCount)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.colorName), u => u.colorName.Contains(input.colorName.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.xilieName), u => u.xilieName.Contains(input.xilieName.Trim()))
                    .WhereIF(input.colorId > 0, u => u.colorId == input.colorId)
                     .Select<car_360Output>();

        if (input.isRandom)
        {
            //  db.Queryable<Student>().Take(10).OrderBy(st => SqlFunc.GetRandom()).ToList();
            query.OrderBy(st => SqlFunc.GetRandom());
        }
        else
        {
            query = query.OrderBuilder(input, "clickCount");
        }


      

        Excar_360Output redata = new Excar_360Output();
        redata.data = query.ToPagedList(input.Page, input.PageSize);

        redata.xilies = _rep.AsQueryable()
                    .WhereIF(input.carId > 0, u => u.carId == input.carId)
                    .WhereIF(input.carSubId > 0, u => u.carSubId == input.carSubId)
                   .Distinct().Select(it => new { it.xilieName, it.xilieId }).ToList();
        return redata;
    }

    /// <summary>
    /// 增加360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_360Input input)
    {
        var entity = input.Adapt<car_360>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_360Input input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_360Input input)
    {
        var entity = input.Adapt<car_360>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取360
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_360> Get([FromQuery] QueryByIdcar_360Input input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取360列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_360Output>> List([FromQuery] car_360Input input)
    {
        return await _rep.AsQueryable().Select<car_360Output>().ToListAsync();
    }





}

